package gui.config;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.event.CaretEvent;

import control.Values;

public class FilterArea extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField jTextField = null;
	private JTextField jTextField1 = null;
	private JTextField jTextField2 = null;
	private EventTable eventTable = null;
	private JLabel jLabel = null;

	/**
	 * This is the default constructor
	 */
	public FilterArea() {
		super();
		this.eventTable = Values.getEventTable();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel = new JLabel();
		jLabel.setText("Filter:");
		FlowLayout flowLayout = new FlowLayout();
		flowLayout.setVgap(0);
		flowLayout.setHgap(0);
		flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
		this.setLayout(flowLayout);
		this.setSize(450, 20);
		this.add(jLabel, null);
		this.add(getJTextField(), null);
		this.add(getJTextField1(), null);
		this.add(getJTextField2(), null);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(72, 20));
			jTextField.addCaretListener(new javax.swing.event.CaretListener() {
				public void caretUpdate(javax.swing.event.CaretEvent e) {
					initFilterArea();
					eventTable.setFilter(jTextField.getText(), 1);
				}
			});
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(66, 20));
			jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
				public void caretUpdate(javax.swing.event.CaretEvent e) {
					initFilterArea();
					eventTable.setFilter(jTextField1.getText(), 2);
				}
			});
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setPreferredSize(new Dimension(260, 20));
			jTextField2.addCaretListener(new javax.swing.event.CaretListener() {
				public void caretUpdate(javax.swing.event.CaretEvent e) {
					initFilterArea();
					eventTable.setFilter(jTextField2.getText(), 3);
				}
			});
		}
		return jTextField2;
	}

	protected void initFilterArea() {
		if(jTextField.isFocusOwner()) {
			jTextField1.setText("");
			jTextField2.setText("");
		}
		if(jTextField1.isFocusOwner()) {
			jTextField.setText("");
			jTextField2.setText("");
		}
		if(jTextField2.isFocusOwner()) {
			jTextField1.setText("");
			jTextField.setText("");
		}
	}

}
